package com.daxue.se;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author daxue0929
 * @date 2022/08/28
 **/
public class StreamTest {





    @Test
    public void test001() {
        List<String> words = new ArrayList<>();
        words.add(" ");
        words.add("daxue");
        words.add("is");
        words.add("good");
        words.add("7");

        // 得到一个[['d','a','x','u','e'], ['i','s'], ['g','o','o','d']], 包含流的流
        Stream<Stream<String>> result = words.stream().map(w -> codePoints(w));

        // 可以将其摊平为单个流
        Stream<String> stringStream = words.stream().flatMap(w -> codePoints(w));

        // 从无限流，裁剪出一个100个随机数
        Stream.generate(Math::random).limit(100);

        codePoints(words.get(1));

        Stream<Object> concatStream = Stream.concat(result, stringStream);


    }




    /**
     * 把字符串分割为单个字符
     * @param s
     * @return
     */
    public Stream<String> codePoints(String s) {
        ArrayList<String> result = new ArrayList<>();
        int i = 0;
        while (i< s.length()) {
            int j = s.offsetByCodePoints(i, 1);
            result.add(s.substring(i, j));
            i = j;
        }
        return result.stream();
    }
}
